/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2015-2-4         yellowlgx
 *
 */

package com.skyworth.ui.api;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.skyworth.commen.ui.R;
import com.skyworth.util.SkyScreenParams;

import java.lang.ref.WeakReference;

/**
 * @ClassName SkyToast
 * @Description <p>
 *              系统统一toast类，为了解决在吕老板首页上android
 *              Toast被遮盖的问题，只能使用WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY属性的dialog来显示
 *              ，所以，这里更改了之前的设计
 *              所有使用到SkyToastView的都需要修改，首先自己工程需要在Manifest.xml上添加相对应权限，为了以后准备建议以下两个都加上： <!--
 *              注册绘制系统层dialog权限 --> <uses-permission
 *              android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission
 *              android:name="android.permission.SYSTEM_OVERLAY_WINDOW" /> public void
 *              setTostString(String toastStr) 此方法为设置Toast提示内容 public void showToast(ShowTime time,
 *              FrameLayout.LayoutParams layoutParams)显示方法 其中time为显示时间枚举类型，分为：LONGTIME(对应时间为4000ms),
 *              SHOTTIME
 *              (对应时间为2000ms)，具体时间不由外部控制了，统一由我这边控制，layoutParams为FrameLayou.LayoutParams，这里需要外部传入
 *              主要是为了控制显示位置，其中长宽建议设置为LayoutParams.WRAP_CONTENT,如果传null默认为剧中显示;
 *              </p>
 * @author yellowlgx
 * @date 2015-2-4
 * @version TODO (write something)
 */
public class SkyToastView extends Dialog
{
    private FrameLayout contentLayout;
    private Context mContext;
    private TextView toastTextView;
    private FrameLayout shadowLayout;
    //    private BlurBgLayout blurBgLayout;
    private FrameLayout.LayoutParams shadowParams;

    private ShowTime mShowTime;

    private int showTime;

    private MyHandler mHandler;

    public enum ShowTime
    {
        LONGTIME, SHOTTIME
    };

    public SkyToastView(Context context)
    {
        super(context, R.style.Lgxdialog_tran);
        this.getWindow().setGravity(Gravity.CENTER);
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        initView(context);
    }

    /**
     * 概述：初始化toast的UI
     * 
     * @date 2015-2-2
     */
    private void initView(Context context)
    {
        if (mHandler == null)
            mHandler = new MyHandler(this);
        mContext = context;

        // contentLayout 全屏透明使用
        contentLayout = new FrameLayout(mContext);
        contentLayout.setFocusable(false);

        // shadowLayout 阴影布局使用
        shadowLayout = new FrameLayout(mContext);
        shadowParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        shadowLayout.setBackgroundResource(R.drawable.ui_sdk_toast_shadow_no_bg);

        //        blurBgLayout = new BlurBgLayout(mContext);
        //        blurBgLayout.setPageType(BlurBgLayout.PAGETYPE.OTHER_PAGE);
        //        blurBgLayout.setBgAlpha(1.0f);
        //        blurBgLayout.setOverLayoutCurAlpha(51);
        //        blurBgLayout.setShadowLayoutAlpha(0.2f);
        //        blurBgLayout.setVisibility(View.INVISIBLE);
        //        shadowLayout.addView(blurBgLayout,
        //                new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        toastTextView = new TextView(mContext);
        toastTextView.setBackgroundColor(0xffe2e2e2);
        FrameLayout.LayoutParams toastTextViewParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        toastTextView.setSingleLine(true);
        toastTextView.setFocusable(false);
        toastTextView.setGravity(Gravity.CENTER);
        toastTextView.setPadding(SkyScreenParams.getInstence(mContext).getResolutionValue(70),
                SkyScreenParams.getInstence(mContext).getResolutionValue(12),
                SkyScreenParams.getInstence(mContext).getResolutionValue(70),
                SkyScreenParams.getInstence(mContext).getResolutionValue(12));
        toastTextView.setTextSize(SkyScreenParams.getInstence(mContext).getTextDpiValue(32));
        toastTextView.setTextColor(mContext.getResources().getColor(R.color.ff5b5b5b));
        toastTextView.setVisibility(View.INVISIBLE);

        shadowLayout.addView(toastTextView, toastTextViewParams);
        contentLayout.addView(shadowLayout, shadowParams);

        this.setContentView(contentLayout,
                new LayoutParams(SkyScreenParams.getInstence(mContext).getResolutionValue(1920),
                        SkyScreenParams.getInstence(mContext).getResolutionValue(1080)));
    }

    /**
     * 概述：设置动画时间
     * 
     * @param ShowTime
     *            枚举参数，设置显示时间
     * @param layoutParams
     *            位置参数，具体位置由外部决定,大小建议直接用LayoutParams.WRAP_CONTENT 传null会显示为默认位置---水平居中靠下距離100
     * 
     * @date 2015-2-2
     */
    @SuppressLint("RtlHardcoded")
    public void showToast(ShowTime time, FrameLayout.LayoutParams layoutParams)
    {

        mShowTime = time;
        if (layoutParams != null)
        {
            shadowParams = layoutParams;
            // shadowParams.bottomMargin -=
            // SkyScreenParams.getInstence(mContext).getResolutionValue(
            // 83);
        } else
        {
            shadowParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT);
            shadowParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
            shadowParams.bottomMargin = SkyScreenParams.getInstence(mContext)
                    .getResolutionValue(17);
        }
        shadowLayout.setLayoutParams(shadowParams);
        toastTextView.setVisibility(View.VISIBLE);

        this.show();
        if (mShowTime.equals(ShowTime.LONGTIME))
        {
            showTime = 4000;
        } else if (mShowTime.equals(ShowTime.SHOTTIME))
        {
            showTime = 2000;
        }
        if (mHandler.hasMessages(0))
            mHandler.removeMessages(0);
        mHandler.sendEmptyMessageDelayed(0, showTime);
    }

    @Override
    public void dismiss()
    {
        if (mHandler.hasMessages(0))
            mHandler.removeMessages(0);
        super.dismiss();
    }

    /**
     * 概述：设置toast显示内容
     * 
     * @param toastStr
     *            toast显示的内容,String
     * @date 2015-2-2
     */
    public void setTostString(String toastStr)
    {
        toastTextView.setText(toastStr);
        //        toastTextView.post(new Runnable()
        //        {
        //            @Override
        //            public void run()
        //            {
        //                FrameLayout.LayoutParams blue_p = (android.widget.FrameLayout.LayoutParams) blurBgLayout
        //                        .getLayoutParams();
        //                blue_p.width = toastTextView.getWidth();
        //                blue_p.height = toastTextView.getHeight();
        //                blurBgLayout.setLayoutParams(blue_p);
        //                blurBgLayout.setVisibility(View.VISIBLE);
        //            }
        //        });
    }

    /**
     * 概述：设置toast显示内容
     * 
     * @param str
     *            toast显示的内容,String
     * @param start
     *            toast显示的内容需要高亮文字起始位置，从0开始计算
     * @param end
     *            toast显示的内容需要高亮结束位置，结束最后一位加1
     * @param color
     *            toast显示的内容需要高亮的颜色
     * @date 2015-2-2
     */
    public void setTostString(String str, int start, int end, int color)
    {
        // 关键字高亮
        SpannableString name = new SpannableString(str);
        CharacterStyle span = new ForegroundColorSpan(color);
        name.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        toastTextView.setText(name);
        //        toastTextView.post(new Runnable()
        //        {
        //            @Override
        //            public void run()
        //            {
        //                FrameLayout.LayoutParams blue_p = (android.widget.FrameLayout.LayoutParams) blurBgLayout
        //                        .getLayoutParams();
        //                blue_p.width = toastTextView.getWidth();
        //                blue_p.height = toastTextView.getHeight();
        //                blurBgLayout.setLayoutParams(blue_p);
        //                blurBgLayout.setVisibility(View.VISIBLE);
        //            }
        //        });
    }

    private static class MyHandler extends Handler
    {
        private final WeakReference<SkyToastView> mView;

        public MyHandler(SkyToastView view)
        {
            mView = new WeakReference<SkyToastView>(view);
        }

        @Override
        public void handleMessage(Message msg)
        {
            try
            {
                SkyToastView view = mView.get();
                if (view != null)
                {
                    switch (msg.what)
                    {
                        case 0:
                            if (view.isShowing())
                                view.dismiss();
                            break;

                        default:
                            break;
                    }
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

}
